/*
 * COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * 2017年12月15日    LongMin         Create the class
 * http://www.jimilab.com/
*/
package com.futuremap.ws.controller;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.futuremap.ws.bean.APIContent;
import com.futuremap.ws.bean.WsContent;
import com.futuremap.ws.constant.CommonConstant;
import com.futuremap.ws.entity.UserAnnotation;
import com.futuremap.ws.entity.UserAnnotationInteractionDto;
import com.futuremap.ws.entity.UserData;
import com.futuremap.ws.entity.WsInfo;
import com.futuremap.ws.service.WebSocketService;

/**
 * @FileName ApiController.java
 * @Description: api服务
 *
 * @Date 2017年12月15日 下午4:08:12
 * @author LongMin
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/api")
public class ApiController extends AbsController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ApiController.class);

	@Autowired
	private WebSocketService webSocketService;
	
	@PostMapping("/wsInfo")
	@ResponseBody
	public APIContent wsInfo(@RequestBody WsInfo info)
			throws IOException {
		try {
			//需要发送的用户
			int userId = info.getUserId();
			String userStr = String.valueOf(userId);
			//需要发送的内容
			sendMsg(userStr,info);
			return apiSuccess(null);
		} catch (Throwable t) {
			LOGGER.error("newAnnotation:error===>",t);
			return apiError(t);
		}
	}
	
	private void sendMsg(String userId, WsInfo info)
	{
		//红点
		sendReadFlagResp(userId,info);
		//负责人以及批注来源
		sendUserList(userId,info);
		//通知提醒
		if(info.getTipInfo() != null)
		{
			sendTips(userId,info);
		}
		//未读互动
		if(info.getInteraction() != null)
		{
			sendUnreadInteraction(userId,info);
		}
	}
	
	private void sendReadFlagResp(String userId, WsInfo info)
	{
		WsContent rs = new WsContent();
		rs.setDataType(CommonConstant.DATA_TYPE_REDFLAG);
		rs.setData(info.getReadFlagResp());
		webSocketService.sendMsg(userId, JSON.toJSONString(rs));
	}
	
	private void sendUserList(String userId, WsInfo info)
	{
		WsContent rs = new WsContent();
		rs.setDataType(CommonConstant.DATA_TYPE_USERLIST);
		UserData ud = new UserData();
		ud.setExecutorList(info.getExecutorList());
		ud.setUserList(info.getUserList());
		rs.setData(ud);
		webSocketService.sendMsg(userId, JSON.toJSONString(rs));
	}
	
	private void sendTips(String userId, WsInfo info)
	{
		WsContent rs = new WsContent();
		rs.setDataType(CommonConstant.DATA_TYPE_TIPS);
		rs.setData(info.getTipInfo());
		webSocketService.sendMsg(userId, JSON.toJSONString(rs));
	}
	
	private void sendUnreadInteraction(String userId, WsInfo info)
	{
		WsContent rs = new WsContent();
		rs.setDataType(CommonConstant.DATA_TYPE_UNREAD_INTERACTION);
		rs.setData(info.getInteraction());
		webSocketService.sendMsg(userId, JSON.toJSONString(rs));
	}
	
	/**
	 * @Title: newAnnotation 
	 * @Description:新批注,暂时没用
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws IOException 
	 * @author Tony
	 * @date 2022年10月17日 下午4:55:59
	 */
	@PostMapping("/annotation")
	@ResponseBody
	public APIContent annotation(@RequestBody UserAnnotation info)
			throws IOException {
		try {
			//需要发送的用户
			int userId = info.getExecutorId();
			//需要发送的内容
			APIContent rs = apiSuccess(info);
			//rs.setDataType(CommonConstant.DATA_TYPE_ANNOTATION);
			webSocketService.sendMsg(String.valueOf(userId), JSON.toJSONString(rs));
			return apiSuccess(null);
		} catch (Throwable t) {
			LOGGER.error("newAnnotation:error===>",t);
			return apiError(t);
		}
	}
	
	/**
	 * @Title: interaction 
	 * @Description:暂时没用
	 * @param info
	 * @return
	 * @throws IOException 
	 * @author Tony
	 * @date 2022年10月25日 下午4:53:17
	 */
	@PostMapping("/interaction")
	@ResponseBody
	public APIContent interaction(@RequestBody UserAnnotationInteractionDto info)
			throws IOException {
		try {
			//需要发送的用户
			int userId = info.getReceiverId();
			//需要发送的内容
			APIContent rs = apiSuccess(info);
			//rs.setDataType(CommonConstant.DATA_TYPE_ANNOTATION_INTERACTIVE);
			webSocketService.sendMsg(String.valueOf(userId), JSON.toJSONString(rs));
			return apiSuccess(null);
		} catch (Throwable t) {
			LOGGER.error("newAnnotation:error===>",t);
			return apiError(t);
		}
	}
}
